<%@ page import="fi.hell.lunchit.Round" %>
<%@ page import="fi.hell.util.CookieService" %>
<%@ page import="fi.hell.lunchit.User" %>
<%@ page import="fi.hell.lunchit.Vote" %>

<g:set var="locale" value="${grailsApplication.config.locale}" />

<div id="panels">
  <g:if test="${flash.message}">
    <div class="message">${flash.message}</div>
  </g:if>
  <div class="homePagePanel">
    <div class="panelTop"></div>
    <div class="panelBody">
      <h1><g:message code="seletions" default="Selections" /></h1>
      <ul>
        <g:each var="r" in="${Round.userSelections()}">
          <li class="controller">
            <g:set var="leftovers" value="${r.yesterdaysLeftOvers()}" />
            <g:link controller="user" action="show" id="${r.user?.id}">${r.user}</g:link> : <g:link controller="restaurant" action="show" id="${r.selection.id}">${r.selection}</g:link>
            <g:if test="${leftovers}">
                <br /><p class="small">(<g:message code="leftovers" args="${[leftovers]}" default="Leftovers from {0}" />)</p>
            </g:if>
          </li>
        </g:each>
      </ul>
    </div>
    <div class="panelBtm"></div>

    <div class="panelTop"></div>
    <div class="panelBody">
      <h1><g:message code="currentStatus" default="Vote status" /></h1>

      <%--g:if test="${currentRound?.confirmed}"--%>
        <ul>
          <g:each var="top" status="i" in="${Round.top()}">
            <li>
              ${i+1}. <g:link controller="restaurant" action="show" id="${top.key.id}">${top.key}</g:link> (${top.value})
              <ul>
                <g:each var="vote" in="${Vote.restaurantVoters(top.key)}">
                  <g:if test="${vote.round.yesterdaysLeftOvers() == ''}">
                    <li class="votersList">- <g:link controller="user" action="show" id="${vote.round.user?.id}">${vote.round.user}</g:link> (${vote.vote})</li>
                  </g:if>
                </g:each>
              </ul>
            </li>
          </g:each>
        </ul>
      <%--/g:if>
      <g:else>
        <g:message code="confirmVotesToSeeVotes" default="Confirm your own votes to see all votes." />
      </g:else--%>
    </div>
    <div class="panelBtm"></div>

    <div class="panelTop"></div>
    <div class="panelBody">
      <h1><g:message code="ownVotes" default="Own votes" /></h1>
      <ul>
        <g:each var="own" status="i" in="${Round.ownVotes()}">
          <li>
          ${i+1}. <g:link controller="restaurant" action="show" id="${own?.restaurant?.id}">${own?.restaurant}</g:link>
          <g:if test="${!own?.round?.confirmed}">
            <a id="confirmRound" href="${createLink(controller:'vote', action:'delete', id:own.id)}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');">x</a>
          </g:if>
          </li>
        </g:each>
      </ul>

      <g:if test="${currentRound?.selection}">
        <br />
        <h1><g:message code="yourSelection" default="Your selection" /></h1>

        <g:if test="${currentRound?.@price == null}">
          <p>${currentRound?.selection}</p>
          <g:form controller="round" action="setPrice">
            <label for="luchPrice"><g:message code="lunchPrice" default="Lunch cost:" /></label>
            <input id="lunchPrice" name="price" type="text" value="" /><g:message code="currency" default="&euro;" /><br />
            <g:if test="${loggedInUser?.company?.luncheonVoucherProfitValue > 0}">
              <label for="luncheonVoucherUsed"><g:message code="luncheonVoucherUsed" default="Luncheon voucher used:" /></label>
              <input id="luncheonVoucherUsed" name="luncheonVoucherUsed" type="checkbox" /><br />
            </g:if>
            <input type="submit" value="${message(code:'ok', default:'OK')}" />
          </g:form>
        </g:if>
        <g:else>
          <p id="enteredLunchPrice">${currentRound?.selection} <g:formatNumber number="${(currentRound?.price/100)}" type="currency" locale="${locale}" currencyCode="EUR" /></p>
        </g:else>
      </g:if>
      <g:if test="${!currentRound?.confirmed && currentVote < 3}">
        <a id="confirmRound" href="${createLink(controller:'round', action:'confirm')}" onclick="return confirm('${message(code: 'default.button.confirmVotes.confirm.message', default: 'Are you sure? You won\\\'t be able to modify your votes after this.')}');"><g:message code="confirmVotes" default="Confirm votes" /></a>
      </g:if>
    </div>
    <div class="panelBtm"></div>
  </div>

  <g:if test="${loggedInUser}">
    <div class="homePagePanel">
      <div class="panelTop"></div>
      <div class="panelBody">
        <h1><g:message code="loggedIn" default="Logged in: {0}" args="${[loggedInUser]}" /></h1>
      </div>
      <div class="panelBtm"></div>
    </div>
  </g:if>
  <g:else>
    <div class="homePagePanel">
      <div class="panelTop"></div>
      <div class="panelBody">
        <h1><g:message code="login" default="Login" /></h1>
        <g:form controller="user" action="login">
          <g:message code="email" default="Email" />:<br />
          <input type="text" name="email" value="" /><br />
          <g:message code="password" default="Password" />:<br />
          <input type="password" name="password" value="" />
          <input type="submit" value="${message(code:'login', default:'Login')}" />
        </g:form>
      </div>
      <div class="panelBtm"></div>
    </div>
  </g:else>
</div>